/**
 * Created by chenzui on 2017/3/28.
 */
var $marketName = $('#marketName');
var $startTime = $('#startTime');
var $allTime = $('#allTime');
var $expDays = $('#expDays');
var $groupSpec = $('#groupSpec');
var $marketDesc = $('#marketDesc');
var $uploadFile = $('#uploadFile');
var imgUrl = "";
var $submitBtn = $('#submitBtn');
function ajaxFileUpload(fileInput){
    var tmp = $(fileInput);
    var id = tmp.attr("id");
    $.ajaxFileUpload({
        url:'/file/upload',            //需要链接到服务器地址
        fileElementId:$(fileInput).attr("id"),
        secureuri: false,
        dataType: 'json',                                                //服务器返回的格式，可以是json
        date:$('#updateForm').serialize(),
        // async:true,
        success: function (data,status){                                 //相当于java中try语句块的用法
            imgUrl = data.data;
            alert("success");
            $('#preview').append($('<img src="http://120.77.86.178:41200'+imgUrl+'"/>'));
        },
        error: function (data, status, e){                               //相当于java中catch语句块的用法
            alert("fail");
        }
    });
    $('#'+id+'').before(tmp);
    $('#'+id+'').remove();
}

function add() {
    var marketName = $marketName.val().trim();
    var theme = $groupSpec.val().trim();
    var marketDesc = $marketDesc.val().trim();
    var expDays = -9999;
    if (imgUrl == ""){
        alert("请选择广告图片");
        return;
    }

    var param = {
        marketName:marketName,
        imgUrl:imgUrl,
        theme:theme,
        marketDesc:marketDesc,
        expDays:expDays
    }

    Ajax.ajax({
        url: "/market/add",
        data: param,
        type: "post",
        dataType: 'json',
        success:function(res){
            console.log(res);
            alert("新增专场成功");
            $('#mainframe').load('/shop/offer/market/list.html');
        }

    });
}

$(function () {
    debugger;
    $startTime.datepicker({
        dateFormat: 'yy-mm-dd'
    });

    $uploadFile.on('change', function() {
        debugger;
        ajaxFileUpload(this);
    });

    $submitBtn.on('click',function () {
        add();
    });
    $('#market-detail-cancel').on('click',function () {
        $('#mainframe').load('/shop/offer/market/list.html');
    });

})